package syt.control;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;



@Controller
@RequestMapping("/URLProxy")
public class URLProxyServlet {
	
	HttpServletResponse lastresponse;
	
	@ModelAttribute("xmlContent")
	@RequestMapping(value = "/getMp3List", method = RequestMethod.POST)
	public String listUsers(HttpServletRequest request,HttpServletResponse response) throws IOException {    

		String title = request.getParameter("title");
		
		title = URLEncoder.encode(title);
		
		//String title = "good";
		String curLine = "";
		String content = "";
		String url="http://box.zhangmen.baidu.com/x?op=12&count=1&title=" + title + "$$";
		URL server=new URL(url);
		
		HttpURLConnection connection = (HttpURLConnection)server.openConnection();;
		connection.connect();
		InputStream is = connection.getInputStream();
		try{
			BufferedReader reader = new BufferedReader(new InputStreamReader(is));
			
			while ((curLine = reader.readLine()) != null) {
			   content = content + curLine + "";
			}
		}finally{
			is.close();
			connection.disconnect();
		}

		return content;
	}
}
